package com.md.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author 刘骄阳
 * 2022-04-26 14:25
 */
@Component
@Slf4j
public class MyGatewayFilter implements GatewayFilter, Ordered {
  /**
   * Process the Web request and (optionally) delegate to the next {@code WebFilter}
   * through the given {@link GatewayFilterChain}.
   *
   * @param exchange the current server exchange
   * @param chain    provides a way to delegate to the next filter
   * @return {@code Mono<Void>} to indicate when request processing is complete
   */
  @Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    log.info("网关过滤器执行~~~");
    ServerHttpRequest request = exchange.getRequest();
    System.out.println(request.getLocalAddress());
    return chain.filter(exchange);
  }

  /**
   * 返回过滤器的优先级,数值越大,优先级越低
   *
   * @return 优先级
   */
  @Override
  public int getOrder() {
    return 7;
  }
}
